package humanMirror.model.analyzer;

import humanMirror.model.human.Human;
import humanMirror.model.human.OneOrganInfo;
import humanMirror.model.human.SomeConstants;

public class OrganAnalyzer implements OrganConstants {

    private int organId;
    private String doctorName;
    private final int LOW_BAD;
    private final int LOW_NORM;
    private final int LOW_GOOD;
    private final int LOW_VERY_GOOD;

    public OrganAnalyzer(int organId, String doctorName, int LOW_BAD, int LOW_NORM, int LOW_GOOD, int LOW_VERY_GOOD) {
        this.doctorName = doctorName;
        this.organId = organId;
        this.LOW_BAD = LOW_BAD;
        this.LOW_NORM = LOW_NORM;
        this.LOW_GOOD = LOW_GOOD;
        this.LOW_VERY_GOOD = LOW_VERY_GOOD;
    }

    private String analyzeOrganHealth(int organHealth) {
        if (organHealth >= LOW_VERY_BAD && organHealth <= SUPER_GOOD) {
            if (organHealth < LOW_BAD)
                return MEANING_LOW_VERY_BAD;

            else if (organHealth < LOW_NORM)
                return MEANING_LOW_BAD;

            else if (organHealth < LOW_GOOD)
                return MEANING_LOW_NORM;

            else if (organHealth < LOW_VERY_GOOD)
                return MEANING_LOW_GOOD;

            else if (organHealth < SUPER_GOOD)
                return MEANING_LOW_VERY_GOOD;

            else //if (organHealth == SUPER_GOOD)
                return MEANING_SUPER_GOOD;
        }
        return MEANING_ERROR;
    }

    public Consultation performOrganConsultation(Human pokemon) {
        OneOrganInfo organInfo = pokemon.getOrganParameters(organId);
        int organHealth = organInfo.get(SomeConstants.Parameters.HEALTH.getParameterNumber());
        StringBuilder parametersMeanings = showOrganParameters(pokemon);
        return new Consultation(doctorName, analyzeOrganHealth(organHealth), parametersMeanings);
    }

    //TODO: add parameter names
    public StringBuilder showOrganParameters(Human pokemon) {
        OneOrganInfo organInfo = pokemon.getOrganParameters(organId);
        StringBuilder result = new StringBuilder();
        SomeConstants.Organs organ = SomeConstants.Organs.numberToOrgan(organId);
        SomeConstants.Parameters[] parameters = SomeConstants.Parameters.getOrganParameters(organ);
        for (int i = 0; i < organInfo.getCountOfParameters(); i++) {
            int parameterMeaning = organInfo.get(i);
            String parameterName = SomeConstants.Parameters.getParameterString(SomeConstants.Parameters.getOrganParameter(parameters, i));
            result.append(parameterName + ": " + parameterMeaning + "\n");
        }
        return result;
    }

    public void setOrganId(int organId) {
        this.organId = organId;
    }
}
